define(['zepto', 'butterfly/view','../../bsl',
    'text!./push.html'
], function($, View, bsl, push) {
    var me = null;

    var view = View.extend({
        events: {
            "tap #back":"back",
            "tap #openDoc":"OpenDoc",
            "tap #requireNotiMsg":"requireNotiMsg",
            "tap #requireLinkMsg":"requireLinkMsg",
            "tap #requireDownMsg":"requireDownMsg",
            "tap #requireTranMsg":"requireTranMsg",
            "tap #getPushInfo":"getPushInfo",
            "tap #createLocalPushMsg":"createLocalPushMsg",
            "tap #listAllPush":"listAllPush",
            "tap #clearAllPush":"clearAllPush",

           
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 监听bslready事件  
        document.addEventListener( "bslready", function(){
            message = document.getElementById("message");
            // 监听点击消息事件
            bsl.push.addEventListener( "click", function( msg ) {
                // 判断是从本地创建还是离线推送的消息
                switch( msg.payload ) {
                    case "LocalMSG":
                        console.info( "点击本地创建消息启动：" );
                    break;
                    default:
                        console.info( "点击离线推送消息启动：");
                    break;
                }
                // 提示点击的内容
                bsl.nativeUI.alert( msg.content );
                // 处理其它数据
                logoutPushMsg( msg );
            }, false );
            // 监听在线消息事件
            bsl.push.addEventListener( "receive", function( msg ) {
                if ( msg.aps ) {  // Apple APNS message
                    console.info( "接收到在线APNS消息：" );
                } else {
                    console.info( "接收到在线透传消息：" );
                }
                logoutPushMsg( msg );
            }, false );
        }, false );           


        },
        pushServer:"http://demo.dcloud.net.cn/helloh5/push/",
        message:null,
        

        /**
         * 日志输入推送消息内容
         */
        logoutPushMsg:function( msg ) {
            console.info( "title: "+msg.title );
            console.info( "content: "+msg.content );
            if ( msg.payload ) {
                if ( typeof(msg.payload)=="string" ) {
                    console.info( "payload(String): "+msg.payload );
                } else {
                    console.info( "payload(JSON): "+JSON.stringify(msg.payload) );
                }
            } else {
                console.info( "payload: undefined" );
            }
            if ( msg.aps ) {
                console.info( "aps: "+JSON.stringify(msg.aps) );
            }
        },

        /**
         * 获取本地推送标识信息
         */
        getPushInfo:function(){
            var info = bsl.push.getClientInfo();
            console.info( "获取客户端推送标识信息：" );
            console.info( "token: "+info.token );
            console.info( "clientid: "+info.clientid );
            console.info( "appid: "+info.appid );
            console.info( "appkey: "+info.appkey );
        },
        /**
         * 本地创建一条推动消息
         */
        createLocalPushMsg:function(){
            var options = {cover:false};
            var str = dateToStr(new Date());
            str += ": 欢迎使用Html5 bsl创建本地消息！";
            bsl.push.createMessage( str, "LocalMSG", options );
            console.info( "创建本地消息成功！" );
            console.info( "请到系统消息中心查看！" );
            if(bsl.os.name=="iOS"){
                console.info('*如果无法创建消息，请到"设置"->"通知"中配置应用在通知中心显示!');
            }
        },
        /**
         * 读取所有推送消息
         */
        listAllPush:function(){
            var msgs=null;
            switch ( bsl.os.name ) {
                case "Android":
                msgs = bsl.push.getAllMessage();
                break;
                default:
                break;
            }
            if ( !msgs ) {
                console.info( "此平台不支持枚举推送消息列表！" );
                return;
            }
            console.info( "枚举消息列表（"+msgs.length+"）：" );
            for ( var i in msgs ) {
                var msg = msgs[i];
                console.info( i+": "+msg.title+" - "+msg.content );
            }
        },
        /**
         * 清空所有推动消息
         */
        clearAllPush:function(){
            bsl.push.clear();
            console.info( "清空所有推送消息成功！" );
        },
        /**
         * 请求‘简单通知’推送消息
         */
        requireNotiMsg:function(){
            var url = me.pushServer+'notiPush.php?appid='+encodeURIComponent(bsl.runtime.appid);
            url += ('&cid='+encodeURIComponent(bsl.push.getClientInfo().clientid));
            url += ('&title='+encodeURIComponent('Hello H5+'));
            url += ('&content='+encodeURIComponent('欢迎回来体验HTML5 bsl应用！'));
            url += ('&version='+encodeURIComponent(bsl.runtime.version));
            bsl.runtime.openURL( url );
        },
        /**
         * 请求‘打开网页’推送消息
         */
        requireLinkMsg:function(){
            var url = me.pushServer+"linkPush.php?appid="+encodeURIComponent(bsl.runtime.appid);
            url += ('&cid='+encodeURIComponent(bsl.push.getClientInfo().clientid));
            url += ('&title='+encodeURIComponent('HBuilder飞一样的编码'));
            url += ('&content='+encodeURIComponent('看HBuilder如何追求代码编写速度的极致！立即去瞧一瞧？'));
            url += ('&url='+encodeURIComponent('http://www.dcloud.io/'));
            url += ('&version='+encodeURIComponent(bsl.runtime.version));
            bsl.runtime.openURL( url );
        },
        /**
         * 请求‘下载链接’推送消息
         */
        requireDownMsg:function(){
            if ( bsl.os.name != "Android" ) {
                bsl.nativeUI.alert( "此平台不支持！" );
                return;
            }
            var url = me.pushServer+'downPush.php?appid='+encodeURIComponent(bsl.runtime.appid);
            url += ('&cid='+encodeURIComponent(bsl.push.getClientInfo().clientid));
            url += ('&title='+encodeURIComponent('Hello H5+'));
            url += ('&content='+encodeURIComponent('新版本发布了！立即下载体验？'));
            url += ('&poptitle='+encodeURIComponent('Hello H5+'));
            url += ('&popcontent='+encodeURIComponent('1. 优化用户体验;\n2. 修复在Android2.3.x某些设备可能异常退出的问题.'));
            url += ('&downtitle='+encodeURIComponent('下载Hello H5+'));
            url += ('&downurl='+encodeURIComponent('http://d.m3w.cn/helloh5p/HelloH5.apk'));
            url += ('&version='+encodeURIComponent(bsl.runtime.version));
            bsl.runtime.openURL( url );
        },
        /**
         * 请求‘透传数据’推送消息
         */
        requireTranMsg:function(){
            var url = me.pushServer+'tranPush.php?appid='+encodeURIComponent(bsl.runtime.appid);
            url += ('&cid='+encodeURIComponent(bsl.push.getClientInfo().clientid));
            url += ('&title='+encodeURIComponent('Hello H5+'));
            url += ('&content='+encodeURIComponent('带透传数据推送通知，可通过bsl.push API获取数据并进行业务逻辑处理！'));
            url += ('&payload='+encodeURIComponent('{title:"Hello H5+ Test",content:"test content",payload:{id:"1234567890"}}'));
            url += ('&version='+encodeURIComponent(bsl.runtime.version));
            bsl.runtime.openURL( url );
        },

        back:function(e){
            window.history.go(-1);
        },
        
        OpenDoc : function(e){
            window.butterfly.navigate('com.no5.cordovademo/doc/push', {
                trigger: true
            });
        },
    });
    return view;
});
